package com.xxh.dasongapp.controller.common;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.dasongapp.common.Result;
import com.xxh.dasongapp.common.param.common.PagePARAM;
import com.xxh.dasongapp.common.validgroup.Insert;
import com.xxh.dasongapp.common.validgroup.Update;
import com.xxh.dasongapp.service.common.IBaseService;
import com.xxh.dasongapp.utils.EmptyToNullUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 *  基础增删查改操作接口，需要定制的覆盖父类方法即可。
 * @param <P> 参数实体类
 * @param <S> Service
 * @param <E> 实体类
 * @param <D> DTO实体类
 */
@Slf4j
public abstract class BaseController<E,D,P, S extends IBaseService<E>> extends AbstractController<E,D,P> {
    @Autowired
    private S baseService;

    public BaseController() {
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/add")
    public Result<D> add(@RequestBody @Validated({Insert.class}) P param) {
        E entity = this.paramToEntity(param);
        baseService.save(entity);
        return Result.success(entityToDto(entity));
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/update")
    public Result<D> update(@RequestBody @Validated({Update.class}) P param) {
        E entity = this.paramToEntity(param);
        baseService.saveOrUpdate(entity);
        return Result.success(entityToDto(entity));
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping(value = "/delete/{id}")
    public Result<String> deleteById(@PathVariable String id) {
        baseService.removeById(id);
        return Result.success(id);
    }

    @ApiOperation(value = "根据id集合删除")
    @DeleteMapping(value = "/delete")
    public Result<List<String>> deleteByIds(@RequestBody List<String> ids) {
        baseService.removeByIds(ids);
        return Result.success(ids);
    }


    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/getById/{id}")
    public Result<D> getById(@PathVariable String id) {
        return Result.success(entityToDto(baseService.getById(id)));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/page")
    public Result<?> getByPage(@RequestBody PagePARAM<P> pagePARAM) throws IllegalAccessException {
        Page<E> page = new Page<E>(pagePARAM.getPageNum(),pagePARAM.getPageSize());
        QueryWrapper<E> qw = new QueryWrapper<>();

        if (StringUtils.isNotBlank(pagePARAM.getOrderBy()) && StringUtils.isNotBlank(pagePARAM.getOrder())){
            if (pagePARAM.getOrder().equals("desc")){
                qw.orderByDesc(pagePARAM.getOrderBy());
            }
            if (pagePARAM.getOrder().equals("asc")){
                qw.orderByAsc(pagePARAM.getOrderBy());
            }
        }

        if (pagePARAM.getSearchPARAM()!=null){
            E e = paramToEntity(pagePARAM.getSearchPARAM());

            EmptyToNullUtils.setNullValue(e);
            qw.setEntity(e);


            return Result.success(this.baseService.page(page,qw));
        }
        return Result.success(this.baseService.page(page,qw));
    }




    protected S getService() {
        return this.baseService;
    }

}
